﻿using System;
using System.Xml;
using Demo.Core.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.Entities;
using VitCode.ObjectResolutionHelper;

namespace VitCode.Web.BaseData.Helper
{
    public class ImportorFactory
    {
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private ImportorFactory()
        {
        }

        /// <summary>
        /// 实例化具体的导入对象(根据有文件名创建对象)
        /// </summary>
        /// <param name="filePath">要导入的EXCEL文件路径</param>
        /// <param name="sheetName">要导入数据的工作表名称</param>
        /// <param name="xmlSettingPath">批量导入配置文件路径</param>
        /// <returns>导入对象</returns>
        public static AbstractImportor ImportorInstance(string filePath, string sheetName, string xmlSettingPath)
        {

            string strFileName = filePath;
            int index = strFileName.LastIndexOf("\\");
            strFileName = strFileName.Substring(index + 1, strFileName.Length - index - 1);
            strFileName = strFileName.ToUpper();
            var helper = new ImportHelper(xmlSettingPath);
            var nvc = helper.GetImprotTypeInfo();
            AbstractImportor reVal=null;
            for(var i=0;i<nvc.Count;i++)
            {
                var strMemo = nvc.GetKey(i);
                if(!string.IsNullOrEmpty(strMemo))
                {
                    var keyString = strMemo + "导入";
                    if(strFileName.IndexOf(keyString) > -1)
                    {
                        var strParms = nvc.Get(i);
                        var ss = strParms.Split(',');
                        var strClassName = ss[0];
                        var strTypeName = "";
                        if (ss.Length == 2) strTypeName = ss[1];
                        reVal = new BaseImportor(filePath, sheetName, strClassName, strTypeName, xmlSettingPath);
                    }
                }
            }
            if(reVal == null)
                throw new ArgumentOutOfRangeException("导入Excel文件的文件名称", "未知的类型，请检查导入的名称是否符合要求");
            return reVal;
        }
    }
}
